/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation;

import java.awt.Color;
import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Date;

import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author kaibiibrahim
 */
public class ProduitForm extends JPanel {

    public ProduitForm() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }

        jLabel1.setVisible(false);
        qteDispField.setVisible(false);
        qteDispLabel.setVisible(false);
    }

    boolean validForm() {
        boolean isInvalid = true;
        boolean isEmpty = true;
        if (libelleField.getText().equalsIgnoreCase("") && libelleField.isEnabled()) {
            libelleField.setBorder(BorderFactory.createLineBorder(Color.RED));
            libelleLabel.setForeground(Color.RED);
            masterTable.setEnabled(false);
            isEmpty = false;
            //jLabel1.setText("Veuillez remplir les champs obligatoires");
        } else {
            libelleField.setBorder(BorderFactory.createEtchedBorder());
            libelleLabel.setForeground(Color.BLACK);
            masterTable.setEnabled(true);
        }
        if (prixAchatField.getText().equalsIgnoreCase("") && prixAchatField.isEnabled()) {
            prixAchatField.setBorder(BorderFactory.createLineBorder(Color.RED));
            prixAchatLabel.setForeground(Color.RED);
            masterTable.setEnabled(false);
            isEmpty = false;
            //jLabel1.setText("Veuillez remplir les champs obligatoires");
        } else {
            try {
                if (prixAchatField.isEnabled()) {
                    Double.parseDouble(prixAchatField.getText());
                }
                prixAchatField.setBorder(BorderFactory.createEtchedBorder());
                prixAchatLabel.setForeground(Color.BLACK);
                masterTable.setEnabled(true);
            } catch (Exception ex) {
                prixAchatField.setBorder(BorderFactory.createLineBorder(Color.RED));
                prixAchatLabel.setForeground(Color.RED);
                masterTable.setEnabled(false);
                isInvalid = false;
                //jLabel1.setText("Les information saisi sont incorrecte");
            }

        }
        if (prixVendreField.getText().equalsIgnoreCase("") && prixVendreField.isEnabled()) {
            prixVendreField.setBorder(BorderFactory.createLineBorder(Color.RED));
            prixVendreLabel.setForeground(Color.RED);
            masterTable.setEnabled(false);
            isEmpty = false;
            //jLabel1.setText("Veuillez remplir les champs obligatoires");
        } else {

            try {
                if (prixVendreField.isEnabled()) {
                    Double.parseDouble(prixVendreField.getText());
                }
                prixVendreField.setBorder(BorderFactory.createEtchedBorder());
                prixVendreLabel.setForeground(Color.BLACK);
                masterTable.setEnabled(true);
            } catch (Exception ex) {
                prixVendreField.setBorder(BorderFactory.createLineBorder(Color.RED));
                prixVendreLabel.setForeground(Color.RED);
                masterTable.setEnabled(false);
                isInvalid = false;
                //jLabel1.setText("Les information saisi sont incorrecte");
            }
        }

        try {
            if (qteDispField.isEnabled() && !qteDispField.getText().equalsIgnoreCase("")) {
                Double.parseDouble(qteDispField.getText());
            }
        } catch (Exception ex) {
            qteDispField.setBorder(BorderFactory.createLineBorder(Color.RED));
            qteDispLabel.setForeground(Color.RED);
            masterTable.setEnabled(false);
            isInvalid = false;
            //jLabel1.setText("Les information saisi sont incorrecte");
        }

        boolean temp;
        if (!isEmpty) {
            temp = isEmpty;
            jLabel1.setText("Veuillez remplir les champs obligatoires");
        } else {
            temp = isInvalid;
            jLabel1.setText("Les information saisi sont incorrecte");
        }
        jLabel1.setVisible(!temp);
        return temp;
    }

    void emptyFields() {
        libelleField.setText("");
        prixAchatField.setText("");
        prixVendreField.setText("");
        qteDispField.setText("");
    }
    int nbrOpertation = 0;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("gestionPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Produit p");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        deleteButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        qteDispField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        prixAchatField = new javax.swing.JTextField();
        prixVendreField = new javax.swing.JTextField();
        libelleField = new javax.swing.JTextField();
        prixVendreLabel = new javax.swing.JLabel();
        prixAchatLabel = new javax.swing.JLabel();
        libelleLabel = new javax.swing.JLabel();
        qteDispLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        FormListener formListener = new FormListener();

        addComponentListener(formListener);

        masterTable.setVerifyInputWhenFocusTarget(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${libelle}"));
        columnBinding.setColumnName("Libelle");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prixAchat}"));
        columnBinding.setColumnName("Prix Achat");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prixVendre}"));
        columnBinding.setColumnName("Prix Vendre");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${qteDisp}"));
        columnBinding.setColumnName("Qte Disp");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterTable.addHierarchyListener(formListener);
        masterTable.addInputMethodListener(formListener);
        masterTable.addAncestorListener(formListener);
        masterScrollPane.setViewportView(masterTable);
        masterTable.getAccessibleContext().setAccessibleName("");
        masterTable.getAccessibleContext().setAccessibleDescription("");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Détailles :"));

        deleteButton.setText("Delete");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(formListener);

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(formListener);

        newButton.setText("New");
        newButton.addActionListener(formListener);

        qteDispField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.qteDisp}"), qteDispField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), qteDispField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        qteDispField.addFocusListener(formListener);

        saveButton.setText("Save");
        saveButton.addActionListener(formListener);

        prixAchatField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prixAchat}"), prixAchatField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prixAchatField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        prixAchatField.addFocusListener(formListener);

        prixVendreField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.prixVendre}"), prixVendreField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), prixVendreField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        prixVendreField.addFocusListener(formListener);

        libelleField.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        libelleField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.libelle}"), libelleField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), libelleField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        libelleField.addActionListener(formListener);
        libelleField.addFocusListener(formListener);
        libelleField.addInputMethodListener(formListener);

        prixVendreLabel.setForeground(new java.awt.Color(51, 51, 51));
        prixVendreLabel.setText("Prix Vendre:");

        prixAchatLabel.setForeground(new java.awt.Color(51, 51, 51));
        prixAchatLabel.setText("Prix Achat:");

        libelleLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        libelleLabel.setForeground(new java.awt.Color(51, 51, 51));
        libelleLabel.setText("Libelle:");

        qteDispLabel.setForeground(new java.awt.Color(51, 51, 51));
        qteDispLabel.setText("Qte Disp:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("Veuillez remplir les champs obligatoires");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(newButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(deleteButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(refreshButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(saveButton))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(prixVendreLabel)
                                .addComponent(qteDispLabel)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(libelleLabel)
                                    .addComponent(prixAchatLabel)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(libelleField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 648, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(prixVendreField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                                    .addComponent(prixAchatField, javax.swing.GroupLayout.Alignment.LEADING))
                                .addComponent(qteDispField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 701, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deleteButton, newButton, refreshButton, saveButton});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(libelleField, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(libelleLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prixAchatLabel)
                    .addComponent(prixAchatField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prixVendreLabel)
                    .addComponent(prixVendreField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(qteDispLabel)
                    .addComponent(qteDispField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(refreshButton)
                    .addComponent(deleteButton)
                    .addComponent(newButton)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 772, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.ComponentListener, java.awt.event.FocusListener, java.awt.event.HierarchyListener, java.awt.event.InputMethodListener, javax.swing.event.AncestorListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == deleteButton) {
                ProduitForm.this.deleteButtonActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton) {
                ProduitForm.this.refreshButtonActionPerformed(evt);
            }
            else if (evt.getSource() == newButton) {
                ProduitForm.this.newButtonActionPerformed(evt);
            }
            else if (evt.getSource() == saveButton) {
                ProduitForm.this.saveButtonActionPerformed(evt);
            }
            else if (evt.getSource() == libelleField) {
                ProduitForm.this.libelleFieldActionPerformed(evt);
            }
        }

        public void componentHidden(java.awt.event.ComponentEvent evt) {
        }

        public void componentMoved(java.awt.event.ComponentEvent evt) {
        }

        public void componentResized(java.awt.event.ComponentEvent evt) {
        }

        public void componentShown(java.awt.event.ComponentEvent evt) {
            if (evt.getSource() == ProduitForm.this) {
                ProduitForm.this.formComponentShown(evt);
            }
        }

        public void focusGained(java.awt.event.FocusEvent evt) {
            if (evt.getSource() == qteDispField) {
                ProduitForm.this.qteDispFieldFocusGained(evt);
            }
            else if (evt.getSource() == prixAchatField) {
                ProduitForm.this.prixAchatFieldFocusGained(evt);
            }
            else if (evt.getSource() == prixVendreField) {
                ProduitForm.this.prixVendreFieldFocusGained(evt);
            }
            else if (evt.getSource() == libelleField) {
                ProduitForm.this.libelleFieldFocusGained(evt);
            }
        }

        public void focusLost(java.awt.event.FocusEvent evt) {
            if (evt.getSource() == qteDispField) {
                ProduitForm.this.qteDispFieldFocusLost(evt);
            }
            else if (evt.getSource() == prixAchatField) {
                ProduitForm.this.prixAchatFieldFocusLost(evt);
            }
            else if (evt.getSource() == prixVendreField) {
                ProduitForm.this.prixVendreFieldFocusLost(evt);
            }
            else if (evt.getSource() == libelleField) {
                ProduitForm.this.libelleFieldFocusLost(evt);
            }
        }

        public void hierarchyChanged(java.awt.event.HierarchyEvent evt) {
            if (evt.getSource() == masterTable) {
                ProduitForm.this.masterTableHierarchyChanged(evt);
            }
        }

        public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
        }

        public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
            if (evt.getSource() == masterTable) {
                ProduitForm.this.masterTableInputMethodTextChanged(evt);
            }
            else if (evt.getSource() == libelleField) {
                ProduitForm.this.libelleFieldInputMethodTextChanged(evt);
            }
        }

        public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
        }

        public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            if (evt.getSource() == masterTable) {
                ProduitForm.this.masterTableAncestorMoved(evt);
            }
        }

        public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    @SuppressWarnings("unchecked")
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed

        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
        nbrOpertation = 0;
        this.validForm();
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int[] selected = masterTable.getSelectedRows();
        List<entity.Produit> toRemove = new ArrayList<entity.Produit>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            entity.Produit p = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(p);
            entityManager.remove(p);
        }
        list.removeAll(toRemove);
        nbrOpertation += selected.length;
        this.validForm();
    }//GEN-LAST:event_deleteButtonActionPerformed
    boolean valid = true;
    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        if (!valid && !this.validForm()) {
            return;
        }

        entity.Produit p = new entity.Produit();
        p.setQteDisp(0);
        entityManager.persist(p);
        list.add(p);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));

        this.emptyFields();
        nbrOpertation += 1;
        valid = this.validForm();
    }//GEN-LAST:event_newButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        if (!validForm()) {
            return;
        }

        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            if (nbrOpertation > 0) {
                JOptionPane.showMessageDialog(this, "L'opération est terminé avec succée", "Information", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<entity.Produit> merged = new ArrayList<entity.Produit>(list.size());
            for (entity.Produit p : list) {
                //p.setQteDisp(0);
                merged.add(entityManager.merge(p));
            }
            list.clear();
            list.addAll(merged);
        }
        nbrOpertation = 0;
    }//GEN-LAST:event_saveButtonActionPerformed

    private void libelleFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_libelleFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_libelleFieldActionPerformed

    private void masterTableInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_masterTableInputMethodTextChanged

    }//GEN-LAST:event_masterTableInputMethodTextChanged

    private void libelleFieldInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_libelleFieldInputMethodTextChanged

    }//GEN-LAST:event_libelleFieldInputMethodTextChanged
    String libelleValue;
    private void libelleFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_libelleFieldFocusGained
        // TODO add your handling code here:
        libelleValue = libelleField.getText();
    }//GEN-LAST:event_libelleFieldFocusGained

    private void libelleFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_libelleFieldFocusLost
        // TODO add your handling code here:
        if (!libelleValue.equals(libelleField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_libelleFieldFocusLost

    private void masterTableAncestorMoved(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_masterTableAncestorMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_masterTableAncestorMoved

    private void masterTableHierarchyChanged(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_masterTableHierarchyChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_masterTableHierarchyChanged
    String prixAchatValue;
    private void prixAchatFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_prixAchatFieldFocusGained
        // TODO add your handling code here:
        prixAchatValue = prixAchatField.getText();
    }//GEN-LAST:event_prixAchatFieldFocusGained

    private void prixAchatFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_prixAchatFieldFocusLost
        // TODO add your handling code here:
        if (!prixAchatValue.equals(prixAchatField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_prixAchatFieldFocusLost
    String prixVendreValue;
    private void prixVendreFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_prixVendreFieldFocusGained
        // TODO add your handling code here:
        prixVendreValue = prixVendreField.getText();
    }//GEN-LAST:event_prixVendreFieldFocusGained

    private void prixVendreFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_prixVendreFieldFocusLost
        // TODO add your handling code here:
        if (!prixVendreValue.equals(prixVendreField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_prixVendreFieldFocusLost
    String qteDisValue;
    private void qteDispFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_qteDispFieldFocusGained
        // TODO add your handling code here:
        qteDisValue = qteDispField.getText();
    }//GEN-LAST:event_qteDispFieldFocusGained

    private void qteDispFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_qteDispFieldFocusLost
        // TODO add your handling code here:
        if (!qteDisValue.equals(qteDispField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_qteDispFieldFocusLost

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // TODO add your handling code here:
        refreshButton.doClick();
    }//GEN-LAST:event_formComponentShown

//    Override void initComponents() {
//        this.initComponents();
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField libelleField;
    private javax.swing.JLabel libelleLabel;
    private java.util.List<entity.Produit> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton;
    private javax.swing.JTextField prixAchatField;
    private javax.swing.JLabel prixAchatLabel;
    private javax.swing.JTextField prixVendreField;
    private javax.swing.JLabel prixVendreLabel;
    private javax.swing.JTextField qteDispField;
    private javax.swing.JLabel qteDispLabel;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ProduitForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProduitForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProduitForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProduitForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new ProduitForm());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

}
